package com.ld.common.util.filelistener.commonio.plugin;

import java.util.List;

import com.ld.admin.model.system.NoticeActionModel;
import com.ld.admin.model.system.filelistener.SysFilelistenerModel;
import com.ld.admin.service.system.filelistener.SysFileLinstenerService;
import com.ld.common.plugin.Plugin;
import com.ld.common.plugin.PluginInfo;
import com.ld.common.util.filelistener.commonio.FileChangedListenerManager;
import com.ld.common.util.filelistener.commonio.FileChangedListenerModel;
import com.ld.common.util.filelistener.commonio.impl.ProjectFileChangedHandler;
import com.ld.shieldsb.common.core.collections.ListUtils;
import com.ld.shieldsb.common.core.model.Result;
import com.ld.shieldsb.dao.model.QueryModel;

import lombok.extern.slf4j.Slf4j;

/**
 * 文件监控插件，千万不要在定时任务 or 文件变动的回调方法中抛出异常！！！参考https://blog.csdn.net/liuyueyi25/article/details/79292971
 * 
 * @ClassName ProjectFileListenerUtil
 * @author <a href="mailto:donggongai@126.com" target="_blank">吕凯</a>
 * @date 2019年2月25日 下午2:11:39
 *
 */
@Slf4j
public class ProjectFileListenerPlugin implements Plugin {
    private PluginInfo pluginInfo = new PluginInfo("项目文件变化监听插件", this); // 插件信息
    protected static SysFileLinstenerService service = new SysFileLinstenerService();

    public static Result update(SysFilelistenerModel model, NoticeActionModel noticActionModel) {
        FileChangedListenerModel listenerModel = FileChangedListenerManager.get(model.getKeyCode());
        listenerModel.setName(model.getName());
        listenerModel.setPath(model.getPath());
        listenerModel.setInterval(model.getIntervals());
        listenerModel.setAllowedExtensions(model.getAllowedExtensions());
        listenerModel.setExclusionRex(model.getExclusionRex());
        ProjectFileChangedHandler changeHandler = (ProjectFileChangedHandler) listenerModel.getListener();
        changeHandler.setModel(model);
        changeHandler.setNoticeActionModel(noticActionModel);
        return FileChangedListenerManager.updateListener(model.getKeyCode(), listenerModel);
    }

    public static Result register(SysFilelistenerModel model, NoticeActionModel noticActionModel) {
        return FileChangedListenerManager
                .register(new FileChangedListenerModel(model.getName(), model.getKeyCode(), model.getPath(), model.getIntervals(),
                        model.getAllowedExtensions(), model.getExclusionRex(), new ProjectFileChangedHandler(model, noticActionModel)));
    }

    @Override
    public void initializePlugin() {
        // 查询所有文件监听并将其启动
        List<SysFilelistenerModel> modelList = service.getIsStartList(SysFilelistenerModel.class, 100); // 最多监控100个任务
        if (ListUtils.isNotEmpty(modelList)) {
            for (SysFilelistenerModel model : modelList) {
                QueryModel queryNoticeModel = new QueryModel();
                queryNoticeModel.addEq("pkey", model.getId());
                queryNoticeModel.addEq("keyCode", "sys_filelistener");
                NoticeActionModel noticActionModel = service.getOne(NoticeActionModel.class, queryNoticeModel); // 通知配置
                Result result = register(model, noticActionModel);
                if (result.getSuccess()) {
                    try {
                        FileChangedListenerManager.start(model.getKeyCode());
                    } catch (Exception e) {
                        log.error("文件监控器(" + model.getName() + ")启动失败", e);
                    }
                }
            }
        } else {
            log.warn("未发现文件监控器！");
        }
    }

    @Override
    public void destroyPlugin() {
        FileChangedListenerManager.shutdown(); // 关闭
    }

    @Override
    public PluginInfo getPluginInfo() {
        return pluginInfo;
    }

}
